<?php
/**
 * Documentation, License etc.
 *
 * @package girafplace
 */

require_once 'include.php';
require_once '_functions.php';

if(!$loggedin)
  die("You must be logged in to access this page");


if(!isset($_POST['name']))
  die("You should only access this file through the edit file dialog");

$id = $_POST['id'];
$name = addslashes($_POST['name']);
$description = addslashes($_POST['description']);

if (isset($_POST['abilities'])) {
	if (is_array($_POST['abilities'])) {
		foreach ($_POST['abilities'] as $abilityId) {
			$abilities[$abilityId] = '1';
		}
	} else {
		throw new Exception('HACKER!!!');
	}
} else {
	$abilities = array();
}

$oldAbilities = $_POST['oldabilities'];

try {
	Connect();

	updateApplication($id,$name,$description);

	if (isset($_FILES['apk'])) {
		uploadAPK($id);
		
		$file = $id . '.apk';
		$apkInfo = getInfoFromApk($file, $id);

		$packageName = $apkInfo['package'];
		$version = (int) $apkInfo['version'];
		$versionString = $apkInfo['versionString'];

		addApplicationPost($id, $package, $version, $versionString);
	}
	
	setAbilities($id, $abilities, $oldAbilities);

	Close();

	header( 'Location: index.php' ) ;
} catch (Exception $e) {
	echo $e->getMessage();
}